package com.example.blogapi.service;

import com.example.blogapi.dao.BlogDao;
import com.example.blogapi.dao.CategoryDao;
import com.example.blogapi.dao.UserDao;
import com.example.blogapi.model.Blog;
import com.example.blogapi.model.Category;
import com.example.blogapi.model.User;

import java.sql.SQLException;
import java.util.List;

public class BlogService {
    private final BlogDao blogDao;
    private final UserDao userDao;
    private final CategoryDao categoryDao;

    public BlogService() {
        this.blogDao = new BlogDao();
        this.userDao = new UserDao();
        this.categoryDao = new CategoryDao();
    }

    public List<Blog> getAllBlogs() throws SQLException {
        return blogDao.findAll();
    }

    public Blog getBlogById(Long id) throws SQLException {
        return blogDao.findById(id);
    }

    public List<Blog> getBlogsByUserId(Long userId) throws SQLException {
        // 验证用户是否存在
        User user = userDao.findById(userId);
        if (user == null) {
            throw new IllegalArgumentException("User not found");
        }
        
        return blogDao.findByUserId(userId);
    }

    public List<Blog> getBlogsByCategoryId(Long categoryId) throws SQLException {
        // 验证分类是否存在
        Category category = categoryDao.findById(categoryId);
        if (category == null) {
            throw new IllegalArgumentException("Category not found");
        }
        
        return blogDao.findByCategoryId(categoryId);
    }

    public List<Blog> getBlogsByTagId(Long tagId) throws SQLException {
        return blogDao.findByTagId(tagId);
    }

    public Blog createBlog(Blog blog, List<Long> tagIds) throws SQLException {
        // 验证用户是否存在
        User user = userDao.findById(blog.getUserId());
        if (user == null) {
            throw new IllegalArgumentException("User not found");
        }
        
        // 验证分类是否存在
        Category category = categoryDao.findById(blog.getCategoryId());
        if (category == null) {
            throw new IllegalArgumentException("Category not found");
        }
        
        return blogDao.create(blog, tagIds);
    }

    public Blog updateBlog(Long id, Blog blog, List<Long> tagIds) throws SQLException {
        Blog existingBlog = blogDao.findById(id);
        if (existingBlog == null) {
            throw new IllegalArgumentException("Blog not found");
        }
        
        // 验证分类是否存在
        Category category = categoryDao.findById(blog.getCategoryId());
        if (category == null) {
            throw new IllegalArgumentException("Category not found");
        }
        
        blog.setId(id);
        blog.setUserId(existingBlog.getUserId()); // 不允许更改博客作者
        blog.setCreatedAt(existingBlog.getCreatedAt());
        
        return blogDao.update(blog, tagIds);
    }

    public boolean deleteBlog(Long id) throws SQLException {
        Blog existingBlog = blogDao.findById(id);
        if (existingBlog == null) {
            throw new IllegalArgumentException("Blog not found");
        }
        
        return blogDao.delete(id);
    }
}